package org.restoran.controller.web;

/**
 * Created with IntelliJ IDEA.
 * User: Andrew Kononenko
 * Date: 18.11.13
 * Time: 16:56
 */

import org.restoran.model.Item;
import org.restoran.service.ItemService;
import org.restoran.util.Utills;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;

@Controller
@RequestMapping(value = "/item", method = RequestMethod.GET, consumes = {"text/plain", "application/*"})
public class ItemController extends AbstractController {

    private static Logger logger = LoggerFactory.getLogger(Utills.getClassName());


    private final ItemService itemService;

    @Autowired
    public ItemController(ItemService itemService) {
        this.itemService = itemService;
    }


    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody String getAllItems()
    {
        logger.debug("Request to all exist now items");

        Iterable<Item> all = itemService.findAll();
        Collection<Item> items = Utills.collection(all);
        String s = itemService.parseToJson(items);
        return success(s);
    }

    @RequestMapping(value = "/{itemId}")
    public @ResponseBody String findById(
            @PathVariable("itemId")
            Integer id)
    {
        Item item = itemService.findOne(id);
        String json = itemService.parseToJson(item);

        logger.debug("return json response {}", json);

        return success(json);
    }

    @RequestMapping (value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity<String> updateOne(
            @RequestBody
            String json)
    {
        Item item = itemService.parseFromJson(json, Item.class);

        logger.debug("deserializer object {}, from JSON and try update", item, json);

        Item updated = itemService.save(item); //todo: Change to update()
        String respJson = itemService.parseToJson(updated);

        return new ResponseEntity<>(success(respJson), HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.POST)
    public  ResponseEntity<String> addOne(
            @RequestBody
            String json)
    {
        Item parsedItem = itemService.parseFromJson(json, Item.class);

        logger.debug("deserializer object {} from JSON {}, and try create new", parsedItem, json);

        this.inspectEntityId(parsedItem);
        Item created = itemService.save(parsedItem);
        String result = itemService.parseToJson(created);

        return new ResponseEntity<>(success(result), HttpStatus.CREATED);
    }



    @RequestMapping(value = "/{itemId}", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(
            @PathVariable("itemId")
            Integer itemId)
    {
        itemService.delete(itemId);
        return new ResponseEntity<>(success(), HttpStatus.OK);
    }

    
    
}
